package com.neusoft.movie.v_movies.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.neusoft.movie.v_movies.R;
import com.neusoft.movie.v_movies.adpater.CommentListAdapter;
import com.neusoft.movie.v_movies.adpater.MoviesAdapter;
import com.neusoft.movie.v_movies.common.Constants;
import com.neusoft.movie.v_movies.javabean.CollectStatus;
import com.neusoft.movie.v_movies.javabean.CommentBean;
import com.neusoft.movie.v_movies.javabean.MoviesBean;
import com.neusoft.movie.v_movies.javabean.ResultBean;
import com.neusoft.movie.v_movies.listener.RetrofitListener;
import com.neusoft.movie.v_movies.model.CollectStateModel;
import com.neusoft.movie.v_movies.model.CommentsModel;
import com.neusoft.movie.v_movies.model.MoviesModel;
import com.neusoft.movie.v_movies.model.UsersModel;
import com.squareup.picasso.Picasso;

import java.util.List;

import static com.neusoft.movie.v_movies.common.Constants.PHOTO_URL;

public class MoviesInfoActivity extends BaseActivity implements RetrofitListener {
    private RecyclerView rv;
    private CommentListAdapter commentListAdapter;
    private TextView m_name,m_actor,m_director,m_time,t_name,m_info;
    private ImageView m_pic,left,collect_movie,button_comment;
    private int m_id,comment_flag,comment_id,collect_id,collect_flag;
    private Context mContext;
    private String result;
    private int u_id;
    private EditText com_content;
    @Override
    void initViews() {
        setLayout(R.layout.activity_movies_info);

        rv = (RecyclerView) findViewById(R.id.rv_comment);
        rv.setLayoutManager(new LinearLayoutManager(this));

        m_name = (TextView) findViewById(R.id.m_name);
        m_pic = (ImageView) findViewById(R.id.m_pic);
        m_actor = (TextView) findViewById(R.id.m_actor);
        m_director = (TextView) findViewById(R.id.m_director);
        m_time = (TextView) findViewById(R.id.m_time);
        t_name = (TextView) findViewById(R.id.t_name);
        m_info = (TextView) findViewById(R.id.m_info);


        com_content= (EditText) findViewById(R.id.com_content);
//        u_name = (TextView) findViewById(R.id.u_name);
//        com_time = (TextView) findViewById(R.id.com_time);
//        com_content = (TextView) findViewById(R.id.com_content);

        collect_movie = (ImageView) findViewById(R.id.collect_movie);
        button_comment=(ImageView) findViewById(R.id.button_comment);
        left=(ImageView) findViewById(R.id.left_button);

    }

    @Override
    void initData() {
        m_id=getIntent().getIntExtra("m_id",-1);
        SharedPreferences sharedPreferences=getSharedPreferences("userInfo", Context.MODE_PRIVATE);
         u_id=sharedPreferences.getInt("u_id",0);
        getDetail();
        collectstate();

        getCommentlist();
    }
    private void getDetail()
    {
        MoviesModel moviesModel = new MoviesModel();
        moviesModel.getDetail(m_id,this);
    }
    private void getCommentlist()
    {

        CommentsModel commentsModel = new CommentsModel();
        commentsModel.getCommentlist(m_id,3,this);
    }


   private void collectstate()
   {
       CollectStateModel collectStateModel=new CollectStateModel();
       collectStateModel.collectstate(u_id,m_id,3,this);
   }



    private void addcollect()
    {
        UsersModel usersModel = new UsersModel();
        usersModel.addcollect(u_id,m_id,3,this);
    }

    private void addcomments()
    {
        UsersModel usersModel = new UsersModel();
        usersModel.addcomments(u_id,
                m_id,
                3,
                com_content.getText().toString(),this);

    }

    @Override
    void initEvents() {
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        collect_movie.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addcollect();
            }
        });

        button_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addcomments();
            }
        });

    }

    @Override
    public void onSuccess(Object o, int flag) {
         switch(flag){
             case Constants.GETMOVIESDETAIL:
                 MoviesBean  moviesBean=(MoviesBean) o;
                 String pic = PHOTO_URL+moviesBean.getM_pic();
                 if(!moviesBean.getM_pic().equals("")){
                     Picasso.with(mContext).load(pic).into(m_pic);
                 }

                 m_name.setText(moviesBean.getM_name());
                 m_actor.setText(moviesBean.getM_actor());
                 m_director.setText(moviesBean.getM_director());
                 m_time.setText(moviesBean.getM_time());
                 t_name.setText(moviesBean.getT_name());
                 m_info.setText(moviesBean.getM_info());
                 break;

             case Constants.GETMOVIESCOMMENTLIST:
                 List<CommentBean> list=(List<CommentBean>)o;
                 CommentListAdapter adapter=new CommentListAdapter(this,list);
                 rv.setAdapter(adapter);
                 break;


             case Constants.ISCOLLECT:
                 //获取收藏状态
                 ResultBean status=(ResultBean)o;
                 result=status.getSuccess();
                 if(status.getSuccess().equals("0")){
                     collect_movie.setImageResource(R.drawable.ic_collect);  //没收藏
                 }
                 else
                 {
                     collect_movie.setImageResource(R.drawable.ic_collect_1);  //收藏
                 }

                 break;


             case Constants.ADDCOLLECT:
                 if (((ResultBean)o).getSuccess().equals("1")) {
                     if (result.equals("0")) {
                         collect_movie.setImageResource(R.drawable.ic_collect_1);
                         Toast.makeText(MoviesInfoActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                         result = "1";
                     } else {
                         collect_movie.setImageResource(R.drawable.ic_collect);
                         Toast.makeText(MoviesInfoActivity.this, "取消收藏成功", Toast.LENGTH_SHORT).show();
                         result = "0";
                     }
                 } else {
                     Toast.makeText(MoviesInfoActivity.this, "操作失败", Toast.LENGTH_SHORT).show();
                 }
                 break;


             case Constants.ADDCOMMENTS:
             if (((ResultBean)o).getSuccess().equals("1")) {
                 Toast.makeText(MoviesInfoActivity.this, "评论成功", Toast.LENGTH_LONG).show();
                 finish();
             } else {
                 Toast.makeText(MoviesInfoActivity.this, "评论失败", Toast.LENGTH_LONG).show();

             }
         }
    }

    @Override
    public void onFailed() {

    }
}
